package _23;

import java.util.*;

public class _List {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("这里有两个集合");
        List<String> lt1 = new ArrayList<>();
        List<String> lt2 = new ArrayList<>();
        String [] array1 = {"a","b","c","d"};
        String [] array2 = {"a","e","f","g"};
        for (int i = 0; i < array1.length; i++){
            lt1.add(array1[i]);
            lt2.add(array2[i]);
        }
        System.out.println("lt1 = " + lt1);
        System.out.println("lt2 = " + lt2);
        while (true) {
            System.out.print("请输入要操作的集合(lt1 or lt2  输入x求两集合的交集   输入exit退出程序): ");
            String chance1 = in.next();
            if (chance1.equals("lt1")) {
                printChance(lt1, in);
            } else if (chance1.equals("lt2")) {
                printChance(lt2, in);
            } else if (chance1.equals("x")){
                System.out.println("交集为：" + getXianTongList(lt1,lt2));
            }else if (chance1.equals("exit")){
                break;
            }
            else {
                System.out.println("输入有误！");
            }
        }
    }
    public static List getXianTongList(List<String> lt1, List<String> lt2){
        List<String> sectionlist = new ArrayList<>();
        for (String item : lt2){      //循环遍历集合lt2
            if (lt1.contains(item)){  //集合lt1如果存在这个数
                sectionlist.add(item);   //将这个数放进集合sectionlist
            }
        }
        return sectionlist;
    }

    public static void printChance(List<String> lt, Scanner in){
        System.out.println("操作前的集合为：" + lt);
        int chance2;
        boolean loop = true;
        while (loop){
            System.out.print("请选择对集合的操作 （输入 1：添加元素  2：删除元素  3：修改元素  4：查询元素  5：返回上一层）：");
            chance2 = in.nextInt();
            switch (chance2) {
                case 1:
                    System.out.println("请输入要添加的元素(输入exit退出）：");
                        while (true) {
                            String s1 = in.next();
                            if (s1.equals("exit")){
                                Collections.sort(lt);
                                System.out.println("操作后的集合为：" + lt);
                                break;
                            }
                            lt.add(s1);
                        }
                    break;
                case 2:
                    System.out.println("请输入要删除的元素(输入exit退出) ：");
                    while (true) {
                        String s2 = in.next();
                        if (s2.equals("exit")){
                            Collections.sort(lt);
                            System.out.println("操作后的集合为：" + lt);
                            break;
                        }
                        lt.remove(s2);
                    }
                    break;
                case 3:
                    System.out.println("请输入要修改的元素和目标元素(输入exit退出) ：");
                    while (true) {
                        String s3 = in.next();
                        String s4 = in.next();
                        if (s3.equals("exit") || s4.equals("exit")){
                            Collections.sort(lt);
                            System.out.println("操作后的集合为：" + lt);
                            break;
                        }
                        Collections.replaceAll(lt, s3, s4);
                    }
                    break;
                case 4:
                    System.out.println("请输入要查询的元素(输入exit退出) ：");
                    while (true) {
                        String s5 = in.next();
                        if (s5.equals("exit")){
                            break;
                        }
                        System.out.println("集合lt1中该元素有" + Collections.frequency(lt, s5) + "个");
                    }
                    break;
                case 5:
                    loop = false;
                    break;
            }
        }
    }
}
